package com.feng.service.Impl;

import com.feng.dao.GoodDao;
import com.feng.dao.TagDao;
import com.feng.dto.Result;
import com.feng.pojo.Good;
import com.feng.pojo.Tag;
import com.feng.service.GoodService;
import com.feng.service.TagService;
import com.feng.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    GoodDao goodDao;
    @Autowired
    TagDao tagDao;
    @Autowired
    TagService tagService;

    @Override
    public Result getRandGood() {
        List<Good> randGood = goodDao.get3Rand();
        return Result.builder().success(true).massage("查找成功").data(randGood).build();
    }

    @Override
    public Result getSearchGood(String searchName) {
        List<Good> searchGood = null;
        Set<Integer> tagIds = tagService.getLevel3Id(searchName);
        searchGood = goodDao.getByNameAndTagId(searchName, tagIds);
        return Result.builder().success(true).massage("查找成功").data(searchGood).build();
    }

    @Override
    public Result getAll() {
        return Result.builder().success(true).massage("成功").data(goodDao.findAll()).build();
    }

    @Override
    public Result create(Map<String, Object> map, Integer sellerId) {
        Good good = new Good();
        good = ObjectUtil.mapToPojo(map, Good.class, good);
        String tagName;
        List<Tag> tags;
        Integer pTagId;
        Integer tagId = null;
        if (!map.containsKey("tagName") || !map.containsKey("pTagId")) {
            return Result.builder().success(false).massage("tagName或者tagId为空").build();
        }
        tagName = map.get("tagName").toString();
        pTagId = (Integer) map.get("pTagId");
        tags = tagDao.getByNameLike(tagName);
        List<Tag> tempTag = tags.stream()
                .filter(tag -> tag.getLevel().equals(3) && tag.getPTag().equals(pTagId))
                .collect(Collectors.toList());
        if (tags.isEmpty() || tempTag.isEmpty()) {
            Tag tag = new Tag(null, tagName, 3, pTagId, null);
            tagDao.create(tag);
            tagId = tag.getId();
        } else {
            tagId = tags.get(0).getId();
        }
        good.setTagId(tagId);
        good.setSellerId(sellerId);
        List<Good> goodByNames = goodDao.getByNameLike(good.getName());
        for (Good goodByName : goodByNames) {
            if (ObjectUtil.isEqualsExceptId(goodByName, good)) {
                return Result.builder().success(false).massage("有一样的商品").build();
            }
        }
        goodDao.create(good);
        return Result.builder().success(true).massage("成功").build();
    }

    @Override
    public Result create2(Map<String, Object> map) {
        List<Tag> tags = tagDao.findAll();
        Good good = new Good();
        good = ObjectUtil.mapToPojo(map, Good.class, good);
        Integer fTagId = createSingleTag("FTag", map, tags, null, 1);
        Integer sTagId = createSingleTag("STag", map, tags, fTagId, 2);
        Integer tTagId = createSingleTag("TTag", map, tags, sTagId, 3);
//        good.setFId(fTagId);
//        good.setSId(sTagId);
//        good.setTId(tTagId);
        List<Good> goods = goodDao.getByNameLike(good.getName());
        if (!goods.isEmpty()) {
            for (Good good1 : goods) {
                good.setId(good1.getId());
                if (!ObjectUtil.isEquals(good1, good)) {
                    good.setId(null);
                    goodDao.create(good);
                    break;
                } else {
                    return Result.builder().success(false).massage("已有该商品").build();
                }
            }
        } else {
            goodDao.create(good);
        }
        return Result.builder().success(true).massage("成功").build();
    }

    public Integer createSingleTag(String tagMark, Map<String, Object> map, List<Tag> tags, Integer pTagId, Integer level) {
        Integer tagId = null;
        Tag tempTag;
        Map<Integer, String> idNameMap = tags.stream()
                .collect(Collectors.toMap(tag -> tag.getId(), tag -> tag.getName()));
        if (map.containsKey(tagMark)) {
            if (!idNameMap.containsValue(map.get(tagMark).toString())) {
                tempTag = new Tag(null, String.valueOf(map.get(tagMark)), level, pTagId, null);
                tagDao.create(tempTag);
                tagId = tempTag.getId();
            } else {
                tagId = tags.stream()
                        .filter(tag -> tag.getName().equals(map.get(tagMark).toString()) && tag.getLevel().equals(level))
                        .map(tag -> tag.getId())
                        .findFirst()
                        .orElse(null);
                if (tagId == null) {
                    tempTag = new Tag(null, String.valueOf(map.get(tagMark)), level, pTagId, null);
                    tagDao.create(tempTag);
                    tagId = tempTag.getId();
                }
            }
        }
        return tagId;
    }

}

